// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

// import sceneMediator from "../resources/framework/sceneMediator";
import gameData from "./gameData";
import sceneMediator from "../framework/sceneMediator";
import Tool from "../framework/Tool";

const {ccclass, property} = cc._decorator;

@ccclass
export default class plotMediator extends cc.Component {

    @property(cc.Node)
    bgNode : cc.Node[] = [];

    @property(cc.Label)
    labNode : cc.Label[] = [];

    onLoad () {
        this.node.zIndex = 1000;
        if(sceneMediator.curEnterLevel == 0){
            if(sceneMediator.getShowScene() == 1){
                this.labNode[1].node.active = false;
                this.labNode[0].string = gameData.getTitleData(sceneMediator.getShowScene(),0);
                this.labNode[1].string = gameData.getTitleData(sceneMediator.getShowScene(),1);
                this.scheduleOnce(()=>{
                    this.loadingAction();
                },2);
            }else{
                this.labNode[0].node.active = false;
                this.labNode[1].node.active = false;
                this.openWindow();
            }
        }else{
            this.labNode[0].node.active = false;
            this.labNode[1].node.active = false;
            this.openWindow();
        }
    }

    closeWindow(){
        cc.tween(this.bgNode[0]).by(1,{
            position : cc.v2(cc.winSize.width / 2,0)
        }).start();

        cc.tween(this.bgNode[1]).by(1,{
            position : cc.v2(-cc.winSize.width / 2,0)
        }).start();
    }

    openWindow(){

        cc.director.getPhysicsManager().enabled = false;

        cc.tween(this.bgNode[0]).by(1,{
            position : cc.v2(-cc.winSize.width / 2,0)
        }).start();

        cc.tween(this.bgNode[1]).by(1,{
            position : cc.v2(cc.winSize.width / 2,0)
        }).call(()=>{
            this.node.destroy();

            if(!gameData.isRelive && !gameData.isRestart && !gameData.backToGame){
                cc.director.getPhysicsManager().enabled = true;
            }else{
                cc.director.getPhysicsManager().enabled = true;
            }

            gameData.backToGame = false;
            gameData.isRelive = false;
            gameData.isRestart = false;

            // if(sceneMediator.curEnterLevel == 1){
            //     if(!gameData.isRelive && !gameData.isRestart && !gameData.backToGame){
            //         cc.director.getPhysicsManager().enabled = true;
            //         cc.game.emit("startShowCheats");
            //     }else{
            //         if(gameData.backToGame){
            //
            //         }else{
            //             Tool.loadPrefab("prefabs/cheats",(res)=>{
            //                 let view = cc.instantiate(res);
            //                 view.parent = cc.find("Canvas");
            //             });
            //         }
            //
            //         cc.director.getPhysicsManager().enabled = true;
            //     }
            //     gameData.backToGame = false;
            //     gameData.isRelive = false;
            //     gameData.isRestart = false;
            // }else{

                //问答界面
                // let list = conversationFrame.getShowScene();
                // if(list.indexOf(sceneMediator.getShowScene()) > -1 && gameData.showQuestion.indexOf(sceneMediator.getShowScene()) < 0){
                //     gameData.showQuestion.push(sceneMediator.getShowScene());
                //     Tool.loadPrefab("prefabs/conversationFrame",(res)=>{
                //         let view = cc.instantiate(res);
                //         view.zIndex = 1000;
                //         view.getComponent("conversationFrame").setCallBack(false,()=>{
                //             if(!gameData.isRelive && !gameData.isRestart){
                //                 Tool.loadPrefab("prefabs/useSkin",(res)=>{
                //                     let view = cc.instantiate(res);
                //                     view.parent = cc.find("Canvas");
                //                 });
                //                 cc.game.emit("startShowCheats");
                //             }else{
                //                 if(sceneMediator.getShowScene() != gameData.isShowCheats){
                //                     Tool.loadPrefab("prefabs/cheats",(res)=>{
                //                         let view = cc.instantiate(res);
                //                         view.parent = cc.find("Canvas");
                //                     });
                //                     gameData.isShowCheats = sceneMediator.getShowScene();
                //                 }
                //                 cc.director.getPhysicsManager().enabled = true;
                //             }
                //             gameData.isRelive = false;
                //             gameData.isRestart = false;
                //         });
                //         view.parent = cc.find("Canvas");
                //     })
                // }else{
                //
                // }


            // }
        }).start();
    }

    loadingAction(){
        this.labNode[1].node.active = true;
        cc.tween(this.labNode[1].node).by(0.3,{
            position : cc.v2(400,0)
        }).call(()=>{
            this.scheduleOnce(()=>{
                cc.tween(this.labNode[1].node).by(0.3,{
                    position : cc.v2(cc.winSize.width / 2 + this.labNode[1].node.width / 2,0)
                }).call(()=>{
                    cc.tween(this.labNode[0].node).by(0.3,{
                        position : cc.v2(cc.winSize.width / 2 + this.labNode[0].node.width / 2,0)
                    }).call(()=>{
                        this.openWindow();
                    }).start();
                }).start();
            },2)
        }).start();
    }

}
